//
//  SelectTimeView.m
//  chongwu
//
//  Created by 隼目 on 2021/9/18.
//

#import "SelectTimeView.h"

@implementation SelectTimeView

-(void)creatView{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    self.hourArr=[NSMutableArray arrayWithCapacity:0];
    self.dayArr=[NSMutableArray arrayWithCapacity:0];
    [self.dayArr addObject:@"今天"];
    [self.dayArr addObject:@"明天"];
    [self.dayArr addObject:@"后天"];
    NSString *dayStr=[ZQTools NSDateToTimeCuo:[NSDate new]];
    for (int i=3; i<5; i++) {
        NSString *typeDay=[NSString stringWithFormat:@"%lld",dayStr.longLongValue+24*3600*i];
        NSString *typeTime=[ZQTools changeTimeCuo:@"M月d日" :typeDay];
        [self.dayArr addObject:typeTime];
    }
    
    NSString *hourStr=[ZQTools changeTimeCuo:@"H"];
    NSString *minutesStr=[ZQTools changeTimeCuo:@"mm"];
    if (minutesStr.intValue>=30) {
        hourStr=[NSString stringWithFormat:@"%ld",hourStr.integerValue+1];
    }
    
    for (int i=hourStr.intValue; i<24; i++) {
        int typeIndex=i+1;
        if (typeIndex==24) {
            typeIndex=0;
        }
        NSString *typeStr=[NSString stringWithFormat:@"%d:00～%d:00",i,typeIndex];
        [self.hourArr addObject:typeStr];
    }
    
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-638*_mainHeight);
    }];
    topView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectRemovew)];
    [topView addGestureRecognizer:tapGesture2];
    
    UIView *botoomView=[UIView new];
    [self addSubview:botoomView];
    [botoomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(topView.mas_bottom);
    }];
    
    UIImageView *bottomImage=[UIImageView new];
    [botoomView addSubview:bottomImage];
    [bottomImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    bottomImage.image=[UIImage imageNamed:@"beijing-1"];
    
    UIView *titleView=[UIView new];
    [botoomView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(133*_mainHeight);
    }];
    
    UILabel *label=[UILabel new];
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    label.text=@"预约上门时间";
    label.textColor=rgba(47,47,47,1);
    label.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(38)];
    
    UIButton *btn=[UIButton new];
    [titleView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-31*_mainWidth);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(_mainWidth*42);
    }];
    [btn setImage:[UIImage imageNamed:@"guanbi"] forState:0];
    [btn addTarget:self action:@selector(selectRemovew) forControlEvents:UIControlEventTouchUpInside];
    UIView *bottomView=[UIView new];
    [botoomView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(181*_mainHeight);
    }];
    
    UIButton *signInBtn=[UIButton new];
    [bottomView addSubview:signInBtn];
    [signInBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(39*_mainWidth);
        make.width.mas_equalTo(_mainW-78*_mainWidth);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(_mainHeight*87);
    }];
    [signInBtn setTitle:@"确定" forState:0];
    [signInBtn setTitleColor:[UIColor whiteColor] forState:0];
    signInBtn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(36)];
    
    [signInBtn layoutIfNeeded];
    signInBtn.layer.masksToBounds=YES;
    signInBtn.layer.cornerRadius=87*_mainHeight/2;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = signInBtn.bounds;
    //将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
    [signInBtn.layer insertSublayer:gradientLayer atIndex:0];
    //设置渐变区域的起始和终止位置（范围为0-1）
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.colors = @[(__bridge id)rgba(255,159,0,1).CGColor,
                             (__bridge id)rgba(255,116,0,1).CGColor ,
                             (__bridge id)rgba(255,102,0,1).CGColor];
    //设置颜色分割点（范围：0-1）
    gradientLayer.locations = @[@(0),@(0.5), @(1.0f)];
    signInBtn.layer.shadowColor = rgba(196,50,0,0.36).CGColor;
    signInBtn.layer.shadowOffset = CGSizeMake(0,0);
    signInBtn.layer.shadowOpacity = 1;
    signInBtn.layer.shadowRadius = 4;
    
    UIView *centerView=[UIView new];
    [botoomView addSubview:centerView];
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(titleView.mas_bottom);
        make.bottom.mas_equalTo(bottomView.mas_top);
    }];
    
    self.pickerView=[UIPickerView new];
    [centerView addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    self.pickerView.delegate=self;
    self.pickerView.dataSource=self;
    

//    NSArray *subviews = self.pickerView.subviews;
//      if (!(subviews.count > 0)) {
//          return;
//      }
    

//    UIView *middleContainerView = subviews[1];
//    NSArray *subviews2 = middleContainerView.subviews;
//    if (middleContainerView) {
//        middleContainerView.backgroundColor = rgba(255,238,226,1);
//    }
    
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    if (component == 0) {
        return self.dayArr.count;
    }

    return self.hourArr.count;
}


// 中指定列和列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return [self.dayArr objectAtIndex:row];;
    }
    return [self.hourArr objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    
    /// 未选中颜色
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.font = [UIFont fontWithName:@"PingFang SC" size:autoScaleW(38)];
        pickerLabel.textColor =rgba(47,47,47,1);
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==0) {
        [self.hourArr removeAllObjects];
        if (row==0) {
            NSString *hourStr=[ZQTools changeTimeCuo:@"H"];
            NSString *minutesStr=[ZQTools changeTimeCuo:@"mm"];
            if (minutesStr.intValue>=30) {
                hourStr=[NSString stringWithFormat:@"%ld",hourStr.integerValue+1];
            }
            
            for (int i=hourStr.intValue; i<24; i++) {
                int typeIndex=i+1;
                if (typeIndex==24) {
                    typeIndex=0;
                }
                NSString *typeStr=[NSString stringWithFormat:@"%d:00～%d:00",i,typeIndex];
                [self.hourArr addObject:typeStr];
            }
        }else{
            for (int i=0; i<24; i++) {
                int typeIndex=i+1;
                if (typeIndex==24) {
                    typeIndex=0;
                }
                NSString *typeStr=[NSString stringWithFormat:@"%d:00～%d:00",i,typeIndex];
                [self.hourArr addObject:typeStr];
            }
        }
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        [self.pickerView reloadComponent:1];
        
    }
}


- (CGFloat)pickerView:(UIPickerView*)pickerView
rowHeightForComponent:(NSInteger)component{
    return 121*_mainHeight;
}
-(void)selectRemovew{
    [self removeFromSuperview];
}

@end
